const app = getApp();
var host = app.globalData.host;
var self = null;

Page({

  data: {
    //tabbar
    tabbar: {},
    isIphoneX: app.globalData.isIphoneX,
    
    userInfo: {},
    showAuth: false,
    currentVer: app.globalData.currentVer
  },

  onLoad(options) {
    //隐藏系统tabbar
    wx.hideTabBar();
    //tabbar调用
    app.editTabbar();
    
    self = this;
    // this.getUserInfo;
    // if (app.globalData.userInfo) {
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true
    //   })
    // } else if (this.data.canIUse) {
    //   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userInfoReadyCallback = res => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // } else {
    //   // 在没有 open-type=getUserInfo 版本的兼容处理
    //   wx.getUserInfo({
    //     success: res => {
    //       app.globalData.userInfo = res.userInfo
    //       this.setData({
    //         userInfo: res.userInfo,
    //         hasUserInfo: true
    //       })
    //     }
    //   })
    // }
    // Do some initialize when page load.
  },
  onShow: function () {
    self.init();
  },

  init() {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          var url = host + '/yq/user';
          var params = {};
          var success = function (res) {
            self.setData({
              userInfo: res.info
            })
            console.log(res.info);
          };
          var fail = function (res) { };
          var complete = function () { };
          app.request(url, params, success, fail, complete);

        } else {
          wx.setStorageSync("path", self.route);
          // wx.reLaunch({
          //   url: '/pages/login/login'
          // })
          self.setData({
            showAuth: true
          })
        }
      }
    })
    // var url = host + '/yq/user';
    // var params = {};
    // var success = function (res) {
    //   console.log(res);
    //   self.setData({
    //     userInfo: res.info
    //   })
    // }
    // var fail = function (res) {};
    // var complete = function (res) {};
    // app.request(url, params, success, fail, complete);
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.checkSession({
        success() {
          var url = host + '/yq/auth/getPhoneNumber';
          var params = {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          };
          var success = function (res) {
            var url = host + '/yq/user/updateMobile';
            var params = {
              mobile: res.info
            };
            var success = function (res) {
              wx.showToast({
                title: '更新手机成功',
                icon: 'none'
              })
            }
            var fail = function (res) {
              wx.showToast({
                title: '更新手机失败',
                icon: 'none'
              })
            };
            var complete = function (res) { };
            app.request(url, params, success, fail, complete);
          }
          var fail = function (res) {
          };
          var complete = function () { };
          app.request(url, params, success, fail, complete);
        },
        fail() {
          // app.loginWechat(this.getPhoneNumber);
        }
      })

    } else {
      wx.showToast({
        title: '获取手机号失败',
        icon: 'none'
      })
    }
  },
  goToPay(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=0'
    })
  },
  goPaid(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=1'
    })
  },
  goPending(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=2'
    })
  },
  goGroup(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=3'
    })
  },
  goCard(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=4'
    })
  },
  goProduct(e) {
    wx.navigateTo({
      url: '/pages/orderList/orderList?type=5'
    })
  },
  goMessage(e) {
    wx.navigateTo({
      url: '/pages/message/message'
    })
  },
  goAddress(e) {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.address']) {
          wx.authorize({
            scope: 'scope.address',
            success() {
              wx.chooseAddress({
                success(res) {
                  console.log(res.userName)
                  console.log(res.postalCode)
                  console.log(res.provinceName)
                  console.log(res.cityName)
                  console.log(res.countyName)
                  console.log(res.detailInfo)
                  console.log(res.nationalCode)
                  console.log(res.telNumber)
                }
              })
            }
          })
        } else {
          wx.chooseAddress({
            success(res) {
              console.log(res.userName)
              console.log(res.postalCode)
              console.log(res.provinceName)
              console.log(res.cityName)
              console.log(res.countyName)
              console.log(res.detailInfo)
              console.log(res.nationalCode)
              console.log(res.telNumber)
            }
          })
        }
      }
    })
  },
  goAbout(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '/pages/web/web?url=' + url
    })
  },
  goAdmin(e) {
    wx.navigateTo({
      url: '/pages/admin/login/login'
    })
  }
})